//
//  NewspaperCategoryView.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-10-29.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NewspaperCategoryView.h"
#import "AppDelegate.h"
#import "NewspaperCategoryCell.h"
#import "NewspaperInfoViewController.h"
@interface NewspaperCategoryView ()
@property (nonatomic, retain) NSArray * newspaperArray;
@end

@implementation NewspaperCategoryView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    novelGridView.requiresSelection = NO;
    novelGridView.layoutDirection = AQGridViewLayoutDirectionHorizontal;//水平显示.
    novelGridView.showsHorizontalScrollIndicator = NO;
}

- (void)setCategory:(NSDictionary *)category
{
    if (category && ![category isEqualToDictionary:_category]) {
        [_category release];
        _category = [category retain];
        [self getData];
    }
}
- (void)getData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getNewspaperPicVersions:[_category objectForKey:@"item_id"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                [self resetDisplay];
                self.newspaperArray = [data objectForKey:@"result"];
                [novelGridView reloadData];
                [self scrollViewDidEndDecelerating:novelGridView];
            }
        });
    });
}

- (void)reloadDataWithArray:(NSArray *)array title:(NSString * )title{
    self.titleStr = title;
    [self resetDisplay];
    self.newspaperArray = [NSArray arrayWithArray:array];
    [novelGridView reloadData];
    [self scrollViewDidEndDecelerating:novelGridView];
}

- (void)resetDisplay
{
//    self.novelArray = nil;
    [novelGridView reloadData];
    leftArrow.hidden = YES;
    rightArrow.hidden = YES;
    NSLog(@"title:%@",self.titleStr);
    nameLabel.text = self.titleStr;
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{

    return _newspaperArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    static NSString *CellIdentify = @"NovelCell";
    NewspaperCategoryCell *cell = (NewspaperCategoryCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[NewspaperCategoryCell alloc] initWithFrame:CGRectMake(0, 0, 70, 100) reuseIdentifier:CellIdentify];
    }
    cell.novelInfo = [_newspaperArray objectAtIndex:index];
    [cell updateDisplayWithImageStr:@"image" text:[[[_newspaperArray objectAtIndex:index] objectForKey:@"page_number"] integerValue]];
    
    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
    return CGSizeMake(94, 100);
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    NewspaperInfoViewController * newspaperInfoViewController = [[[NewspaperInfoViewController alloc] initWithNibName:@"NewspaperInfoViewController" bundle:nil] autorelease];
    newspaperInfoViewController.picVersionArray = _newspaperArray;
    newspaperInfoViewController.baseInfo = [_newspaperArray objectAtIndex:index];
    newspaperInfoViewController.titleStr = self.titleStr;
    newspaperInfoViewController.isHomePage = NO;
    [[UIApplication rootNavController] pushViewController:newspaperInfoViewController animated:YES];
    [gridView deselectItemAtIndex:index animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    leftArrow.hidden = (scrollView.contentOffset.x > 0) ? NO :YES;
    rightArrow.hidden = (scrollView.contentOffset.x < scrollView.contentSize.width - scrollView.bounds.size.width) ? NO :YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        [self scrollViewDidEndDecelerating:scrollView];
    }
}

- (void)dealloc
{
    [_category release];
    [nameLabel release];
    [novelGridView release];
    [_newspaperArray release];
    [leftArrow release];
    [rightArrow release];
    [super dealloc];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
